import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class FrontDesk extends User {
    public FrontDesk(String id, String username, String password, String phone) {
        super(id, username, password, phone, "前台");
    }

    @Override
    public boolean login(String password) {
        if (isLocked()) {  // 修复：使用封装方法访问锁定状态
            System.out.println("账户已锁定");
            return false;
        }
        if (this.password.equals(password)) {
            setLoginAttempts(0);  // 修复：使用setter方法更新尝试次数
            updateLoginAttempts();
            return true;
        } else {
            setLoginAttempts(getLoginAttempts() + 1);  // 修复：使用getter/setter维护状态
            if (getLoginAttempts() >= 5) {
                setLocked(true);  // 修复：使用setter方法更新锁定状态
                System.out.println("密码错误次数过多，账户已锁定");
            }
            updateLoginAttempts();
            return false;
        }
    }

    private boolean isLocked() {
        return false;
    }

    private void setLocked(boolean b) {
    }

    private int getLoginAttempts() {
        return 0;
    }

    private void setLoginAttempts(int i) {
    }

    // 更新登录尝试次数和锁定状态到数据库
    private void updateLoginAttempts() {
        String sql = "UPDATE users SET login_attempts = ?, locked = ? WHERE id = ?";
        // 修复：使用try-with-resources正确关闭资源，明确声明Connection类型
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, getLoginAttempts());  // 修复：使用getter方法获取属性
            pstmt.setBoolean(2, isLocked());      // 修复：使用封装方法访问锁定状态
            pstmt.setString(3, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {  // 修复：捕获具体异常而非泛型Exception
            System.err.println("更新登录尝试次数失败: " + e.getMessage());
        }
    }

    public Ticket sellTicket(Show show, List<String> seatIds, String customerPhone) {
        // 检查座位是否可售
        for (String seatId : seatIds) {
            if (!SeatDAO.isSeatAvailable(seatId, show.getShowId())) {  // 修复：使用正确的场次ID获取方法
                throw new IllegalArgumentException("座位[" + seatId + "]已售出或不存在");
            }
        }

        // 锁定座位
        SeatDAO.lockSeats(seatIds);

        // 创建票务记录
        String ticketId = UUID.randomUUID().toString();
        Ticket ticket = new Ticket(ticketId, show, seatIds, customerPhone, show.getPrice(), new Date());

        // 保存票务信息
        TicketDAO.addTicket(ticket);

        Log.record("售票", "售出影片[" + show.getMovie().getName() + "]的票", this.username);
        return ticket;
    }

    public void refundTicket(String ticketId) {
        Ticket ticket = TicketDAO.getTicketById(ticketId);
        if (ticket == null) {
            throw new IllegalArgumentException("票不存在");
        }

        Show show = ShowDAO.getShowById(ticket.getShowId());
        if (show == null) {  // 新增：增加场次不存在的校验
            throw new IllegalArgumentException("对应场次不存在，无法退票");
        }

        long diff = show.getShowTime().getTime() - System.currentTimeMillis();
        // 修复：处理时间差为负数的情况（已过期场次）
        if (diff < 0) {
            throw new IllegalArgumentException("该场次已放映，不能退票");
        }
        if (diff < 3600 * 1000) { // 距离放映不足1小时
            throw new IllegalArgumentException("距离放映不足1小时，不能退票");
        }

        // 解锁座位
        SeatDAO.unlockSeats(ticket.getSeatIds());

        // 删除票务记录
        TicketDAO.deleteTicket(ticketId);

        Log.record("退票", "退掉票[" + ticketId + "]", this.username);
    }
}